package com.roncoo.education.user.service.api.biz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiDepartmentListResponse.Department;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserSimplelistResponse;
import com.dingtalk.api.response.OapiUserSimplelistResponse.Userlist;
import com.roncoo.education.course.feign.vo.UserTagNameVO;
import com.roncoo.education.user.common.bo.*;
import com.roncoo.education.user.service.ITzxxService;
import com.roncoo.education.user.service.dao.*;
import com.roncoo.education.user.service.dao.service.impl.mapper.UserPMapper;
import com.roncoo.education.util.dingd.DingDUtil;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.aliyuncs.exceptions.ClientException;
import com.roncoo.education.system.feign.vo.SysVO;
import com.roncoo.education.system.feign.interfaces.IFeignSys;
import com.roncoo.education.user.common.bo.auth.UserUpdateBO;
import com.roncoo.education.user.common.dto.TzinfoDTO;
import com.roncoo.education.user.common.dto.UserLoginDTO;
import com.roncoo.education.user.service.dao.impl.mapper.TzxxMapper;
import com.roncoo.education.user.service.dao.impl.mapper.entity.Platform;
import com.roncoo.education.user.service.dao.impl.mapper.entity.SendSmsLog;
import com.roncoo.education.user.service.dao.impl.mapper.entity.Tzxx;
import com.roncoo.education.user.service.dao.impl.mapper.entity.User;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserExt;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserLogLogin;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserTag;
import com.roncoo.education.util.aliyun.Aliyun;
import com.roncoo.education.util.aliyun.AliyunUtil;
import com.roncoo.education.util.base.BaseBiz;
import com.roncoo.education.util.base.BaseException;
import com.roncoo.education.util.base.Page;
import com.roncoo.education.util.base.PageUtil;
import com.roncoo.education.util.base.Result;
import com.roncoo.education.util.enums.IsSuccessEnum;
import com.roncoo.education.util.enums.LoginStatusEnum;
import com.roncoo.education.util.enums.ResultEnum;
import com.roncoo.education.util.enums.StatusIdEnum;
import com.roncoo.education.util.enums.UserTypeEnum;
import com.roncoo.education.util.tools.BeanUtil;
import com.roncoo.education.util.tools.Constants;
import com.roncoo.education.util.tools.DateUtil;
import com.roncoo.education.util.tools.JWTUtil;
import com.roncoo.education.util.tools.NOUtil;
import com.roncoo.education.util.tools.StrUtil;
import com.taobao.api.ApiException;
import com.xiaoleilu.hutool.crypto.DigestUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.RandomUtil;

import javax.annotation.Resource;

/**
 * 用户基本信息
 *
 * @author wujing
 */
@Component
public class ApiUserInfoBiz extends BaseBiz {
	
	@Autowired
	private IFeignSys bossSys;

	@Autowired
	private PlatformDao platformDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserExtDao userExtDao;
	@Autowired
	private SendSmsLogDao sendSmsLogDao;
	@Autowired
	private UserLogLoginDao userLogLoginDao;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Autowired
	private DingDUtil dingDUtil;

	@Resource
	private UserPMapper userPMapper;

	@Autowired
	private UserTagDao userTagDao;
	
	@Autowired
	private TzxxMapper tzxxMapper;
	@Autowired
	private ITzxxService iTzxxService;
	@Transactional
	public Result<UserLoginDTO> register(UserRegisterBO userRegisterBO) {
		if (StringUtils.isEmpty(userRegisterBO.getMobile())) {
			return Result.error("手机号不能为空");
		}
		if (StringUtils.isEmpty(userRegisterBO.getPassword())) {
			return Result.error("密码不能为空");
		}
		if (StringUtils.isEmpty(userRegisterBO.getClientId())) {
			return Result.error("clientId不能为空");
		}

		// 密码校验
		if (!userRegisterBO.getPassword().equals(userRegisterBO.getRepassword())) {
			return Result.error("2次密码不一致");
		}

		Platform platform = platformDao.getByClientId(userRegisterBO.getClientId());
		if (null == platform) {
			return Result.error("该平台不存在");
		}
		if (!StatusIdEnum.YES.getCode().equals(platform.getStatusId())) {
			return Result.error("该平台状态异常，请联系管理员");
		}

		// 验证码校验
		String redisSmsCode = redisTemplate.opsForValue().get(platform.getClientId() + userRegisterBO.getMobile());
		if (StringUtils.isEmpty(redisSmsCode)) {
			return Result.error("请输入验证码");
		}
		if (!redisSmsCode.equals(userRegisterBO.getCode())) {
			return Result.error("验证码不正确，请重新输入");
		}

		// 手机号重复校验
		User user = userDao.getByMobile(userRegisterBO.getMobile());
		if (null != user) {
			return Result.error("该手机号已经注册，请更换手机号");
		}

		// 用户注册
		user = register(userRegisterBO.getMobile(), userRegisterBO.getPassword(), platform.getClientId());

		UserLoginDTO dto = new UserLoginDTO();
		dto.setUserNo(user.getUserNo());
		dto.setMobile(user.getMobile());
		dto.setToken(JWTUtil.create(user.getUserNo(), JWTUtil.DATE));
		return Result.success(dto);
	}

	public Result<UserLoginDTO> loginPassword(UserLoginPasswordBO userLoginPasswordBO) {
		if (StringUtils.isEmpty(userLoginPasswordBO.getClientId())) {
			return Result.error("clientId不能为空");
		}
		if (StringUtils.isEmpty(userLoginPasswordBO.getMobile())) {
			return Result.error("手机号不能为空");
		}
		if (StringUtils.isEmpty(userLoginPasswordBO.getPassword())) {
			return Result.error("密码不能为空");
		}
		Platform platform = platformDao.getByClientId(userLoginPasswordBO.getClientId());
		if (null == platform) {
			return Result.error("该平台不存在");
		}
		if (!StatusIdEnum.YES.getCode().equals(platform.getStatusId())) {
			return Result.error("该平台状态异常，请联系管理员");
		}

		// 密码错误次数校验

		// 用户校验
		User user = userDao.getByMobile(userLoginPasswordBO.getMobile());
		if (null == user) {
			return Result.error("账号或者密码不正确");
		}
		// 密码校验
		if (!DigestUtil.sha1Hex(user.getMobileSalt() + userLoginPasswordBO.getPassword()).equals(user.getMobilePsw())) {
			loginLog(user.getUserNo(), userLoginPasswordBO.getClientId(), LoginStatusEnum.FAIL, userLoginPasswordBO.getIp());
			// 放入缓存，错误次数+1
			return Result.error("账号或者密码不正确");
		}

		// 登录日志
		loginLog(user.getUserNo(), userLoginPasswordBO.getClientId(), LoginStatusEnum.SUCCESS, userLoginPasswordBO.getIp());

		UserLoginDTO dto = new UserLoginDTO();
		dto.setUserNo(user.getUserNo());
		dto.setMobile(user.getMobile());
		dto.setToken(JWTUtil.create(user.getUserNo(), JWTUtil.DATE));

		// 登录成功，存入缓存，单点登录使用
		// redisTemplate.opsForValue().set(dto.getUserNo().toString(), dto.getToken(),
		// 1, TimeUnit.DAYS);

		return Result.success(dto);
	}

	public Result<UserLoginDTO> loginCode(UserLoginCodeBO userLoginCodeBO) {
		if (StringUtils.isEmpty(userLoginCodeBO.getClientId())) {
			return Result.error("clientId不能为空");
		}
		if (StringUtils.isEmpty(userLoginCodeBO.getMobile())) {
			return Result.error("手机号码不能为空");
		}
		Platform platform = platformDao.getByClientId(userLoginCodeBO.getClientId());
		if (ObjectUtil.isNull(platform)) {
			return Result.error("该平台不存在");
		}
		if (!StatusIdEnum.YES.getCode().equals(platform.getStatusId())) {
			return Result.error("该平台状态异常，请联系管理员");
		}

		// 登录错误次数的校验

		// 验证码校验
		String redisSmsCode = redisTemplate.opsForValue().get(platform.getClientId() + userLoginCodeBO.getMobile());
		if (StringUtils.isEmpty(redisSmsCode)) {
			return Result.error("验证码已经过期，请重新获取");
		}

		User user = userDao.getByMobile(userLoginCodeBO.getMobile());
		if (null == user) {
			return Result.error("该用户不存在");
		}

		if (!redisSmsCode.equals(userLoginCodeBO.getCode())) {
			loginLog(user.getUserNo(), userLoginCodeBO.getClientId(), LoginStatusEnum.FAIL, userLoginCodeBO.getIp());
			// 缓存控制错误次数
			return Result.error("验证码不正确,重新输入");
		}

		// 清空缓存
		redisTemplate.delete(platform.getClientId() + userLoginCodeBO.getMobile());

		// 登录日志
		loginLog(user.getUserNo(), userLoginCodeBO.getClientId(), LoginStatusEnum.SUCCESS, userLoginCodeBO.getIp());

		UserLoginDTO dto = new UserLoginDTO();
		dto.setUserNo(user.getUserNo());
		dto.setMobile(user.getMobile());
		dto.setToken(JWTUtil.create(user.getUserNo(), JWTUtil.DATE));

		// 登录成功，存入缓存，单点登录使用
		// redisTemplate.opsForValue().set(dto.getUserNo().toString(), dto.getToken(),
		// 1, TimeUnit.DAYS);
		return Result.success(dto);
	}

	public Result<String> sendCode(UserSendCodeBO userSendCodeBO) {
		if (StringUtils.isEmpty(userSendCodeBO.getClientId())) {
			return Result.error("clientId不能为空");
		}
		if (!Pattern.compile(Constants.REGEX_MOBILE).matcher(userSendCodeBO.getMobile()).matches()) {
			return Result.error("手机号码格式不正确");
		}

		Platform platform = platformDao.getByClientId(userSendCodeBO.getClientId());
		if (ObjectUtil.isNull(platform)) {
			return Result.error("该平台不存在");
		}
		if (!StatusIdEnum.YES.getCode().equals(platform.getStatusId())) {
			return Result.error("该平台状态异常，请联系管理员");
		}

		SysVO sys = bossSys.getSys();
		if (ObjectUtil.isNull(sys)) {
			return Result.error("找不到系统配置信息");
		}
		// 创建日志实例
		SendSmsLog sendSmsLog = new SendSmsLog();
		sendSmsLog.setMobile(userSendCodeBO.getMobile());
		sendSmsLog.setTemplate(sys.getSmsCode());
		// 随机生成验证码
		sendSmsLog.setSmsCode(RandomUtil.randomNumbers(6));
		try {
			// 发送验证码
			boolean result = AliyunUtil.sendMsg(userSendCodeBO.getMobile(), sendSmsLog.getSmsCode(), BeanUtil.copyProperties(sys, Aliyun.class));
			// 发送成功，验证码存入缓存：5分钟有效
			if (result) {
				redisTemplate.opsForValue().set(userSendCodeBO.getClientId() + userSendCodeBO.getMobile(), sendSmsLog.getSmsCode(), 5, TimeUnit.MINUTES);
				sendSmsLog.setIsSuccess(IsSuccessEnum.SUCCESS.getCode());
				sendSmsLogDao.save(sendSmsLog);
				return Result.success("发送成功");
			}
			// 发送失败
			sendSmsLog.setIsSuccess(IsSuccessEnum.FAIL.getCode());
			sendSmsLogDao.save(sendSmsLog);
			throw new BaseException("发送失败");
		} catch (ClientException e) {
			sendSmsLog.setIsSuccess(IsSuccessEnum.FAIL.getCode());
			sendSmsLogDao.save(sendSmsLog);
			logger.error("发送失败，原因={}", e.getErrMsg());
			return Result.error("发送失败");
		}
	}

	private User register(String mobile, String password, String clientId) {
		// 用户基本信息
		User user = new User();
		user.setUserNo(NOUtil.getUserNo());
		user.setMobile(mobile);
		user.setMobileSalt(StrUtil.get32UUID());
		user.setMobilePsw(DigestUtil.sha1Hex(user.getMobileSalt() + password));
		user.setUserSource(clientId);
		userDao.save(user);

		// 用户其他信息
		UserExt userExt = new UserExt();
		userExt.setUserNo(user.getUserNo());
		userExt.setUserType(UserTypeEnum.USER.getCode());
		userExt.setMobile(user.getMobile());
		userExtDao.save(userExt);

		return user;
	}

	private void loginLog(Long userNo, String clientId, LoginStatusEnum status, String ip) {
		UserLogLogin record = new UserLogLogin();
		record.setUserNo(userNo);
		record.setClientId(clientId);
		record.setLoginStatus(status.getCode());
		record.setLoginIp(ip);
		userLogLoginDao.save(record);
	}

	public Result<Integer> updatePassword(UserUpdateBO userUpdateBO) {
		if (StringUtils.isEmpty(userUpdateBO.getMobile())) {
			return Result.error("手机号为空,请重试");
		}
		if (StringUtils.isEmpty(userUpdateBO.getCode())) {
			return Result.error("验证码不能为空");
		}
		if (StringUtils.isEmpty(userUpdateBO.getClientId())) {
			return Result.error("clientId不能为空");
		}

		Platform platform = platformDao.getByClientId(userUpdateBO.getClientId());
		if (null == platform) {
			return Result.error("该平台不存在");
		}
		if (!StatusIdEnum.YES.getCode().equals(platform.getStatusId())) {
			return Result.error("该平台状态异常，请联系管理员");
		}

		String redisCode = redisTemplate.opsForValue().get(platform.getClientId() + userUpdateBO.getMobile());
		if (StringUtils.isEmpty(redisCode)) {
			return Result.error("请输入验证码");
		}
		if (!userUpdateBO.getCode().equals(redisCode)) {
			return Result.error("验证码匹配不正确");
		}
		// 手机号去空处理
		String mobile = userUpdateBO.getMobile().replaceAll(" +", "");

		if (StringUtils.isEmpty(userUpdateBO.getConfirmPassword())) {
			return Result.error("新登录密码为空,请重试");
		}
		if (!userUpdateBO.getNewPassword().equals(userUpdateBO.getConfirmPassword())) {
			return Result.error("密码输入不一致，请重试");
		}

		User user = userDao.getByMobile(mobile);
		if (ObjectUtil.isNull(user)) {
			return Result.error("没找到用户信息,请重试");
		}
		if (DigestUtil.sha1Hex(user.getMobileSalt() + userUpdateBO.getNewPassword()).equals(user.getMobilePsw())) {
			return Result.error("输入的密码与原密码一致,请重试");
		}

		// 更新密码
		User bean = new User();
		bean.setId(user.getId());
		bean.setMobileSalt(StrUtil.get32UUID());
		bean.setMobilePsw(DigestUtil.sha1Hex(bean.getMobileSalt() + userUpdateBO.getNewPassword()));
		int result = userDao.updateById(bean);
		return result == 1 ? Result.success(result) : Result.error(ResultEnum.USER_UPDATE_FAIL.getDesc());
	}
	@Transactional
	public Result<UserLoginDTO> dingdAuthLogin(UserDingdAuthBO dingdAuthBO) throws Exception{

		String accessToken = dingDUtil.getAccessToken();
		if (StringUtils.isEmpty(accessToken)){
			return Result.error("获取钉钉授权失败");
		}
		String userId= dingDUtil.getUserId(dingdAuthBO.getCode(),accessToken);
		if(StringUtils.isEmpty(userId)){
			return Result.error("钉钉授权失败：无此用户");
		}
		OapiUserGetResponse response= dingDUtil.getUserInfo(userId,accessToken) ;
		String unionId =response.getUnionid();
		if (StringUtils.isEmpty(unionId)){
			return Result.error("该用户不存在");
		}
		//如果count >0代表该用户已经注册
		int count=userPMapper.selectCount(new QueryWrapper<User>()
				.eq("dd_id",userId));
		User user=null;
		if(count<1){
			//未注册
			user=newRegister(response);
		}else{
			//已注册 获取用户
			user=userPMapper.selectOne(new QueryWrapper<User>().eq("dd_id",userId).eq("status_id","1"));
		}
		// 登录日志
		loginLog(user.getUserNo(),"lkb65617f842ad4c37895a733b8de43cbb", LoginStatusEnum.SUCCESS,"127.0.0.1");

		UserLoginDTO dto = new UserLoginDTO();
		dto.setUserNo(user.getUserNo());
		dto.setMobile(user.getMobile());
		dto.setToken(JWTUtil.create(user.getUserNo(), JWTUtil.DATE));

		return Result.success(dto);

	}

	private User newRegister(OapiUserGetResponse response) {
		//未注册
		User user= new User();
		user.setDdId(response.getUserid());
		user.setEmail(response.getEmail());
		user.setGmtCreate(new Date());
		user.setGmtModified(new Date());
		user.setLevel(1);
		user.setStatusId(1);
		user.setMobile(response.getMobile());
		user.setMobilePsw("1");
		user.setMobileSalt("1");
		user.setUserSource("lkb65617f842ad4c37895a733b8de43cbb");
		user.setUserType(1);
		user.setUserNo(NOUtil.getUserNo());
		user.insert();

		List<Long> departments=response.getDepartment();
		if (null==departments||departments.size()<1){
			throw new RuntimeException("获取部门信息失败");
		}
		Long departmentId=departments.get(departments.size()-1);

		UserExt userExt=new UserExt();
		userExt.setUserNo(user.getUserNo());
		userExt.setPositions(response.getPosition());
		userExt.setHeadImgUrl(response.getAvatar());
		userExt.setStatusId(1);
		userExt.setNickname(response.getName());
		userExt.setDepartment(String.valueOf(departmentId));
		userExt.setRemark(response.getNickname()+response.getPosition());

		userExt.insert();
		return user;
	}

	public Result<Page<UserExt>> userList(UserExtBo userExt){
		Page<UserExt> page = userExtDao.userListPage(userExt.getPageCurrent(), userExt.getPageSize(), userExt);
		return Result.success(PageUtil.transform(page, UserExt.class));
	}
	
	public Result<Page<UserTag>> getTagNameList(UserTagBo userTag){
		Page<UserTag> page = userExtDao.getTagNameList(userTag.getPageCurrent(), userTag.getPageSize(), userTag);
		return Result.success(PageUtil.transform(page, UserTag.class));
	}
	
	public Result<HashedMap<String, Object>> userInfo() throws ApiException {
		String accessToken = dingDUtil.getAccessToken();
		if (StringUtils.isEmpty(accessToken)){
			return Result.error("获取钉钉授权失败");
		}
		Map<String, Object> map = new HashedMap<String, Object>();
		List<Userlist> userList = new ArrayList<Userlist>();
		OapiDepartmentListResponse departments = dingDUtil.getDepartments(accessToken);
		List<Department> department = departments.getDepartment();
		for (Department departmentItem :department) {
			OapiUserSimplelistResponse userInfo = dingDUtil.getUserInfo(accessToken, departmentItem.getId());
			List<Userlist> userInfolist = userInfo.getUserlist();
			userList.addAll(userInfolist);
		}
		map.put("department", department);
		map.put("userList", userList);
		return Result.success(new HashedMap<String, Object>(map));
	}
	
	public Result<ArrayList<TzinfoDTO>> getUserInfo(TzinfoBO tzinfoBO){
		String accessToken = dingDUtil.getAccessToken();
		if (StringUtils.isEmpty(accessToken)){
			return Result.error("获取钉钉授权失败");
		}
		List<TzinfoDTO> tzinfoDTOList = new ArrayList<TzinfoDTO>();
		//查询名称
		if (org.apache.commons.lang3.StringUtils.isNotEmpty(tzinfoBO.getRealName()) && org.apache.commons.lang3.StringUtils.isEmpty(tzinfoBO.getDepartment())) {
			OapiUserGetResponse getuser = dingDUtil.getuser(accessToken,tzinfoBO.getRealName());
			TzinfoDTO tzinfoDTO = new TzinfoDTO();
			tzinfoDTO.setRealName(getuser.getName());
			tzinfoDTOList.add(tzinfoDTO);
			return Result.success(new ArrayList<TzinfoDTO>(tzinfoDTOList));
		}
		//查部门
		if (org.apache.commons.lang3.StringUtils.isNotEmpty(tzinfoBO.getDepartment()) && org.apache.commons.lang3.StringUtils.isEmpty(tzinfoBO.getRealName())) {
			try {
				OapiUserSimplelistResponse userInfo = dingDUtil.getUserInfo(accessToken, Long.valueOf(tzinfoBO.getDepartment()));
				List<Userlist> userInfolist = userInfo.getUserlist();
				for (Userlist UserItem : userInfolist) {
					TzinfoDTO tzinfoDTO = new TzinfoDTO();
					tzinfoDTO.setRealName(UserItem.getName());
					tzinfoDTOList.add(tzinfoDTO);
				}
				return Result.success(new ArrayList<TzinfoDTO>(tzinfoDTOList));
			} catch (NumberFormatException | ApiException e) {
				e.printStackTrace();
			}
			
		}
		//多条件查询
		if (org.apache.commons.lang3.StringUtils.isNotEmpty(tzinfoBO.getDepartment()) && org.apache.commons.lang3.StringUtils.isNotEmpty(tzinfoBO.getRealName())) {
			OapiUserGetResponse getuser = dingDUtil.getuser(accessToken,tzinfoBO.getRealName());
			List<Long> department = getuser.getDepartment();
			Boolean flag = false;
			for (Long item : department) {
				if (tzinfoBO.getDepartment().equals(item+"")) {
					flag = true;
					break;
				}
			}
			if(flag) {
				OapiUserGetResponse getuser1 = dingDUtil.getuser(accessToken,tzinfoBO.getRealName());
				TzinfoDTO tzinfoDTO = new TzinfoDTO();
				tzinfoDTO.setRealName(getuser1.getName());
				tzinfoDTOList.add(tzinfoDTO);
				return Result.success(new ArrayList<TzinfoDTO>(tzinfoDTOList));
			}else {
				//该人员不在该部门
				return Result.success(new ArrayList<TzinfoDTO>(), "该人员不在部门");
			}
		}
		return null;
	}


	public Result<ArrayList<UserTagNameVO>> userLabel(Long userNo) {
		ArrayList<UserTagNameVO> list = userTagDao.userLabel(userNo);
		if (list.size()>0) {
			return Result.success(list);
		}
		return Result.error("该用户不存在");
	}
	
	public Result<Integer> addUserInfo(AddTzInfoBO addTzInfoBO){
		Tzxx tzxx = new Tzxx();
		tzxx.setContext(addTzInfoBO.getContext());
		String realName = "";
		for (String realNameItem : addTzInfoBO.getRealName()) {
			realName = realName+realNameItem+",";
		}
		realName = realName.substring(0, realName.length() - 1);
		tzxx.setRealName(realName);
		tzxx.setIsTimeSend(addTzInfoBO.getIsTimeSend());
		if (addTzInfoBO.getIsTimeSend() == 1) {
			//定时发送
			tzxx.setSendTime(addTzInfoBO.getSendTime());
		}else {
			//立即发送
			tzxx.setSendTime(DateUtil.getDateTime(new Date()));
		}
		tzxxMapper.insert(tzxx);
		return Result.success(ResultEnum.SUCCESS.getCode());
	}
	
	public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<Tzxx>> selectTzxx(PageHaperBO pageHaperBO){
		com.baomidou.mybatisplus.extension.plugins.pagination.Page<Tzxx> page = iTzxxService.page(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<Tzxx>(pageHaperBO.getPageCurrent(), 
				pageHaperBO.getPageSize()), new QueryWrapper<Tzxx>());
		return Result.success(page);
	}
	
	public Result<Tzxx> selectSendTzxx(Integer id){
		return Result.success(tzxxMapper.selectById(id));
	}
	
	public Result<Integer> editSendTzxx(EditTzInfoBO editTzInfoBO){
		Tzxx tzxx = getTzxx(editTzInfoBO);
		if (iTzxxService.updateById(tzxx)) {
			return Result.success(ResultEnum.SUCCESS.getCode());
		}
		return Result.error(ResultEnum.ERROR.getCode(), "编辑失败");
	}
	 
	private Tzxx getTzxx(EditTzInfoBO editTzInfoBO) {
		Tzxx tzxx = new Tzxx();
		tzxx.setContext(editTzInfoBO.getContext());
		tzxx.setId(editTzInfoBO.getId());
		tzxx.setIsTimeSend(1);
		tzxx.setSendTime(editTzInfoBO.getSendTime());
		String realName = "";
		for (String realNameItem : editTzInfoBO.getRealName()) {
			realName = realName+realNameItem+",";
		}
		realName = realName.substring(0, realName.length() - 1);
		tzxx.setRealName(realName);
		return tzxx;
	}
	
	public Result<Integer> delTzInfo(Integer id){
		if (iTzxxService.removeById(id)) {
			return Result.success(ResultEnum.SUCCESS.getCode());
		}
		return Result.error(ResultEnum.ERROR.getCode().toString());
	}
}
